package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.dao.TbAgentProductDao;
import com.thinkgem.jeesite.modules.sys.dao.TbAgentProductMapper;
import com.thinkgem.jeesite.modules.sys.dao.TbShopProductDao;
import com.thinkgem.jeesite.modules.sys.entity.TbAgentProduct;
import com.thinkgem.jeesite.modules.sys.entity.TbProduct;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by loutong on 2016/11/3.
 */
@Service
public class TbAgentProductService {
    @Autowired
    private TbAgentProductDao tbAgentProductDao;

    @Autowired
    private TbAgentProductMapper tbAgentProductMapper;
   @Autowired
    private TbShopProductDao tbShopProductDao;

    /**
     * 查对应代理商下的所有商品信息
     */

    @Transactional(readOnly = true)
    public Page<TbAgentProduct> findList(Page<TbAgentProduct> tbProductPage, TbAgentProduct tbAgentProduct){
        tbAgentProduct.setPage(tbProductPage);
        if(tbAgentProduct.getTitle()!=null){
            String str = "%";
            str += tbAgentProduct.getTitle();
            str += "%";
            tbAgentProduct.setTitle(str);
        }
      /*  if(StringUtils.isEmpty(tbAgentProduct.getReserveFlag())){
            tbAgentProduct.setReserveFlag("4");
        }*/
        List list=tbAgentProductDao.findList(tbAgentProduct);
        for(int i=0;i<list.size();i++){
            TbAgentProduct  t=(TbAgentProduct)list.get(i);
        }
       tbProductPage.setList(list);
        return tbProductPage;
    }
    @Transactional(readOnly = true)
    public Page<TbProduct> findAgentReserveProductList(Page<TbProduct> tbProductPage, TbProduct tbProduct){
        tbProduct.setPageNo(tbProductPage.getPageNo()-1);
        tbProduct.setPageSize(tbProductPage.getPageSize());
        tbProductPage.setCount(tbAgentProductDao.findAgentReserveProductListcount(tbProduct));
        List list=tbAgentProductDao.findAgentReserveProductList(tbProduct);
        tbProductPage.setList(list);
        return tbProductPage;
    }

    /**
     *根据商户ID和代理商ID查TbAgentProduct
     */
    @Transactional(readOnly = true)
    public TbAgentProduct  get(TbAgentProduct tbAgentProduct){
        List<TbAgentProduct> tbAgentProduct1=tbAgentProductMapper.getAgentProductByProIdAndAgentId(tbAgentProduct.getProductId(),tbAgentProduct.getAgentId());
        return tbAgentProduct1.get(0);
    }

    /**
     * 修改
     */
    public int update(TbAgentProduct tbAgentProduct){
        return tbAgentProductMapper.updateByPrimaryKey(tbAgentProduct);
    }

    /**
     * 通过代理商ID查代理商下的所有商品
     */
    public List<TbAgentProduct> queryByAgentId(String agentId){
        return tbAgentProductDao.queryByAgentId(agentId);
    }
    //判断该商品是否上架
    public void updateStatus(TbProduct tbProduct) {
        TbAgentProduct tbAgentProduct = new TbAgentProduct();
        tbAgentProduct.setId(tbProduct.getId());
        tbAgentProduct.setStatus(tbProduct.getStatus());
        tbAgentProductMapper.updateStatus(tbAgentProduct);
    }

    public TbAgentProduct selectByProductId(Integer id) {
        return tbAgentProductMapper.getAgentProductByProIdAndAgentId(id, UserUtils.getUser().getId()).get(0);
    }

    public TbAgentProduct selectById(String productId) {
        return  tbAgentProductMapper.selectByPrimaryKey(Integer.parseInt(productId));
    }

    public boolean checkNum(Integer id,Integer shopNum){
        TbAgentProduct tbAgentProduct=tbAgentProductDao.findStoreNum(id);
        if(tbAgentProduct!=null&&tbAgentProduct.getStoreNum()!=null){
            if(shopNum>tbAgentProduct.getStoreNum()){
                //如果要进货的数量大于库存，则提示库存不足
                return false;
            }else{
                return true;
            }
        }
        return true;
    }
}
